import { defineStore } from "pinia";
import type { SchoolState } from "./types/type";
import type { schoolAddResponseData, schoolAllResponseData, schoolInfoData, schoolPageData, schoolPageResponseData, schoolTotalData, shcoolRemoveResponseData } from "@/api/school/type";
import { reqSchoolAdd, reqSchoolAll, reqSchoolFindById, reqSchoolPage, reqSchoolRemove, reqSchoolUpdate } from "@/api/school";

const useSchoolStore = defineStore("School",{
    state:():SchoolState=>{
        return{
            
        }
    },
    actions:{
        async addSchool(data:schoolInfoData){
            let result:schoolAddResponseData = await reqSchoolAdd(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async findById(_id:string){
            let result:schoolAddResponseData = await reqSchoolFindById(_id);
            if(result.status == 0){
                return result.data as schoolInfoData;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async updateSchool(data:schoolInfoData){
            let result:schoolAddResponseData = await reqSchoolUpdate(data);
            if(result.status == 0){
                return "ok";
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async allSchool(){
            let result:schoolAllResponseData = await reqSchoolAll();
            if(result.status == 0){
                return result.data as schoolInfoData[];
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async pageSchool(data:schoolPageData){
            let result:schoolPageResponseData = await reqSchoolPage(data);
            if(result.status == 0){
                let pageData:schoolTotalData = result.data as schoolTotalData;
                return pageData;
            }else{
                return Promise.reject(new Error(result.msg))
            }
        },
        async removeSchool(schoolId:string){
            let result:shcoolRemoveResponseData = await reqSchoolRemove(schoolId);
            if(result.status == 0){
                return "ok";
            }else{
                return Promise.reject(new Error(result.msg))
            }
        }
        
    }
})
export default useSchoolStore;